/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Generate;

import define.ClassDefine;
import define.PartialDefine;
import define.ProjectDefine;
import define.TemplateDefine;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import source.AttributeSource;
import source.ClassSource;
import source.ParamSource;
import source.ProjectSource;

/**
 *
 * @author RoderickNestor
 */
public class Generator {

    ProjectSource projectsource;
    ProjectDefine projectdefine;
    String path_tpl = "";

    public Generator(String path_xml_source, String path_xml_define) {

        File file = new File(path_xml_source);
        if (!file.exists()) {
            System.err.println("Fichero XML no existe");
            //return null;
        }
        //leer xml
        InputStream is = null;
        Reader reader = null;
        try {
            is = new FileInputStream(path_xml_source);
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            System.out.println("Reding xml...");
        } catch (FileNotFoundException | UnsupportedEncodingException ex) {
            System.err.println("Error al leer fichero XML : " + ex);
        }
        //Poblar objecto Project        
        JAXBContext jc;
        try {
            jc = JAXBContext.newInstance(ProjectDefine.class);
            Unmarshaller u = jc.createUnmarshaller();
            projectsource = (ProjectSource) u.unmarshal(reader);
            System.out.println("Built project...");
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

        //----------------------------------------------------------------------
        file = new File(path_xml_define);
        if (!file.exists()) {
            System.err.println("Fichero XML no existe");
            //return null;
        }
        //leer xml
        is = null;
        reader = null;
        try {
            is = new FileInputStream(path_xml_define);
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            System.out.println("Reding xml...");
        } catch (FileNotFoundException | UnsupportedEncodingException ex) {
            System.err.println("Error al leer fichero XML : " + ex);
        }
        //Poblar objecto Project        
        //JAXBContext jc;
        try {
            jc = JAXBContext.newInstance(ProjectDefine.class);
            Unmarshaller u = jc.createUnmarshaller();
            projectdefine = (ProjectDefine) u.unmarshal(reader);
            System.out.println("Built project...");
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }
    }

    public void proces() {
        for (ClassSource source : projectsource.list) {
            Map<String, String> replace = new HashMap<>();;
            ClassDefine define = projectdefine.getClassDefine(source.type);
            //lista de los templates que usa
            for (TemplateDefine template : define.templates) {
                //lista de partial que usa
                List<PartialDefine> partials = define.getPartialForTemplate(template.name);
                for (PartialDefine partial : partials) {
                    LoadFile tpl_partial = new LoadFile(path_tpl + File.separator + partial.path);
                    tpl_partial.replace("class_name", "");
                    for (ParamSource param : source.params) {
                        tpl_partial.replace_main(param.name, param.value);
                    }
                    for (AttributeSource attr : source.atrrs) {
                        tpl_partial.replace("attr_name", attr.name);
                        for (ParamSource param : attr.params) {
                            tpl_partial.replace("attr_" + param.name, param.value);
                        }

                        if (replace.containsKey(partial.name)) {
                            String o = replace.get(partial.name).toString();
                            replace.put(partial.name, o + tpl_partial.getPtl());
                        } else {
                            replace.put(partial.name, tpl_partial.getPtl());
                        }
                    }

                }

            }
        }
    }
}
